import React, { Component, Fragment } from 'react';
import { WeaDialog, WeaUpload, WeaLocaleProvider, WeaTools } from 'ecCom';
import { Button, Progress,message } from 'antd';

const { getLabel } = WeaLocaleProvider;

export default class UploadFileComponent extends Component {
  constructor(props) {
    super(props);
    this.showBtn = false;
    this.odocfileType = '0';
    this.docid = '0';
    this.WfForm = window.WfForm || null;
    this.docInfo = this.WfForm.getOdocConfig();
    this.state = {
      maxsize: 5,
      uploadType: 0,
      uploadProcess: 0,
      uploading: false,
    };
  }

  componentDidMount () {
    const uploadComponent = document.getElementById('uploadComponent');
    if (uploadComponent.getAttribute('showBtn')) {
      this.showBtn = true;
    }
    if (uploadComponent.getAttribute('docid')) {
      this.docid = uploadComponent.getAttribute('docid');
    }
    if (uploadComponent.getAttribute('odocfileType')) {
      this.odocfileType = uploadComponent.getAttribute('odocfileType');
    }

    const data = {
      secid: this.docInfo.docParam.secid,
      workflowid: this.docInfo.docParam.workflowid,
    };
    WeaTools.callApi('/api/odoc/uploadPDF/getMaxSize', 'POST', data).then((res) => {
      this.setState({ maxsize: res.maxsize, uploadType: res.uploadType });
    });
  }

  render () {
    const { uploading, uploadProcess, uploadType } = this.state;
    const canUploadSimpleOdoc =this.docInfo.docParam.canUploadSimpleOdoc;
    return (
        <div id="uploadPDFcomponent">
          <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@k5bx5r`}
                     title="上传文件"
                     icon="icon-coms-official"
                     onCancel={this.removeDialog}
                     visible={true}
                     style={{ width: 500, height: 300 }}
                     hasScroll
                     maskClosable={true}
                     parentClassName="uploadPDFcomponent"
          >
            {uploading &&
            <div className="processInfo">
              <Progress ecId={`${this && this.props && this.props.ecId || ''}_Progress@0m1gm0`} percent={uploadProcess} />
            </div>
            }
            <div className="uploadButtons"
                 style={{ display: !uploading ? 'block' : 'none' }}
            >
              <div className="defaultBtns">
                {
                  this.showBtn == true&& canUploadSimpleOdoc&&
                  <Fragment>
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ykcczq`} onClick={this.uploadText} className="margin20">{getLabel(382551, '新建正文')}</Button>

                  </Fragment>
                }
              </div>

              <div className="uploadOtherBtns" >
                {
                    (this.odocfileType == 2||this.odocfileType == 3 || this.showBtn == true||this.odocfileType == 'word') &&
                  <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@3zhx4m`}
                             btnSize="small"
                             uploadUrl="/api/doc/upload/uploadFile"
                             ref={refs => this.upload = refs}
                             category="string"
                             maxFilesNumber={1}
                             maxUploadSize={this.state.maxsize}
                             listType="list"
                             limitType={'doc,docx,wps'}
                             clearWhenReset={false}
                             showClearAll={true}
                             onChange={this.onChange}
                             getShowListDatas={this.getShowList}
                             style={{ marginTop: '20px' }}
                  >
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pkr6ul`} className="uploadBtn">{getLabel(501148, '上传word')}</Button>
                  </WeaUpload>
                }
                {
                  (uploadType == '1' || uploadType == '2') && (this.odocfileType == 12 || this.showBtn == true||this.odocfileType == 'PDF') &&
                  <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@3zhx4m`}
                             btnSize="small"
                             uploadUrl="/api/doc/upload/uploadFile"
                             ref={refs => this.upload = refs}
                             category="string"
                             maxFilesNumber={1}
                             maxUploadSize={this.state.maxsize}
                             listType="list"
                             limitType={'pdf'}
                             clearWhenReset={false}
                             showClearAll={true}
                             onChange={this.onChange}
                             getShowListDatas={this.getShowList}
                             style={{ marginTop: '20px' }}
                  >
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pkr6ul`} className="uploadBtn">{getLabel(501148, '上传PDF')}</Button>
                  </WeaUpload>
                }

                {
                  (uploadType == '1' || uploadType == '3') && (this.odocfileType == 13 || this.showBtn == true||this.odocfileType == 'OFD') &&
                  <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@kkiovy`}
                             btnSize="small"
                             uploadUrl="/api/doc/upload/uploadFile"
                             ref={refs => this.upload = refs}
                             category="string"
                             maxFilesNumber={1}
                             maxUploadSize={this.state.maxsize}
                             listType="list"
                             limitType="ofd"
                             clearWhenReset={false}
                             showClearAll={true}
                             onChange={this.onChange}
                             getShowListDatas={this.getShowList}
                             style={{ marginTop: '20px' }}
                  >
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5kgtxz`} className="uploadBtn">{getLabel(510619, '上传OFD')}</Button>
                  </WeaUpload>
                }

              </div>
            </div>
          </WeaDialog>
        </div>

    );
  }

  onChange = (ids = [], list) => {

    const limitType = list[0] && list[0].fileExtendName;
    this.upload.props.style.display = 'none';
    const params = {
      imagefileid: ids,
      secid: this.docInfo.docParam.secid,
      docid: this.docid,
      workflowid: this.docInfo.docParam.workflowid,
      requestid: this.docInfo.docParam.requestid,
      nodeid: this.docInfo.docParam.nodeid,
      docType: '.' + limitType,
    };

    WeaTools.callApi('/api/odoc/uploadPDF/getPDFDocId', 'POST', params).then((data) => {
      this.upload.props.style.display = 'block';
      if (data.api_status) {

        this.docInfo.docParam.pdffileId = ids;
        const documentTitleField = this.WfForm.getOdocConfig().docParam.documentTitleField;
        const documentTitle = WfForm.getFieldValue(`field${documentTitleField}`);
        const canUploadSimpleOdoc =this.docInfo.docParam.canUploadSimpleOdoc;
        const isUseMB =this.docInfo.docParam.isUseMB;
        this.WfForm.changeOdocConfig(this.docInfo);
        if(isUseMB){
         const result=  oDocUtil.setOfdDatas(ids[0]);
        if(result||canUploadSimpleOdoc){
          this.WfForm.changeFieldValue(`field${this.docInfo.docParam.docFieldId}`, {
            value: data.docid,
            specialobj: [
              { id: data.docid, name: data.docname },
            ],
          });
          if (documentTitle === '') {
            this.WfForm.changeFieldValue(`field${documentTitleField}`, {
              value: data.docname,
            });
          }
          this.WfForm.doSave();
          this.removeDialog();
        }else{
          message.error("非正式定密文件不允许上传!");
          this.removeDialog();
        }

      }else{
        this.WfForm.changeFieldValue(`field${this.docInfo.docParam.docFieldId}`, {
          value: data.docid,
          specialobj: [
            { id: data.docid, name: data.docname },
          ],
        });
        if (documentTitle === '') {
          this.WfForm.changeFieldValue(`field${documentTitleField}`, {
            value: data.docname,
          });
        }
        this.WfForm.doSave();
        this.removeDialog();
      }}
    });
  }

  removeDialog = () => {
    const req_workflow_odoc = document.getElementsByClassName('req-workflow-odoc');
    if (req_workflow_odoc.length > 0) {
      req_workflow_odoc[0].style.width = '100%';
    }
    const uploadPDFParent = parent.document.getElementsByClassName('uploadPDFcomponent')[0];
    const uploadComponent = document.getElementById('uploadComponent');
    oDocUtil.uploadPDFCanClick = true;
    uploadComponent.parentNode.removeChild(uploadComponent);
    uploadPDFParent.parentNode.removeChild(uploadPDFParent);
    showOrHideTextInForm(false);
  }

  uploadText = () => {
    oDocUtil.uploadPDFCanClick = true;
    oDocUtil.clickAddOdoc(true);
    this.removeDialog();
  }

  getLimitType (odocfileType, uploadType) {
    let limitType = 'pdf';
    if (odocfileType == 12 || odocfileType == 'PDF' || uploadType == 2) {
      limitType = 'pdf';
      return limitType;
    } else if (odocfileType == 13 || odocfileType == 'OFD' || uploadType == 3) {
      limitType = 'ofd';
      return limitType;
    }
    return limitType;
  }

  getShowList = (listT, listB) => {
    if (listB[0]) {
      this.setState({ uploadProcess: listB[0].progress, uploading: true });
    } else {
      this.setState({ uploadProcess: 0, uploading: false });
    }
  }
}
